package com.lucien.system.service.impl;

import com.lucien.system.domain.SysApply;
import com.lucien.system.domain.SysApplyExample;
import com.lucien.system.domain.SysUser;
import com.lucien.system.mapper.SysApplyMapper;
import com.lucien.system.service.SysApplyService;
import com.lucien.system.service.SysRelationShipService;
import com.lucien.system.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysApplyServiceImpl implements SysApplyService {

    @Autowired
    private SysApplyMapper applyMapper;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRelationShipService shipService;

    /**
     * 发送好友申请
     * @param userId    接受者ID
     * @return
     */
    @Override
    public int sendApply(int userId) {
        SysApply apply = new SysApply();
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();

        SysApplyExample example = new SysApplyExample();
        SysApplyExample.Criteria criteria = example.createCriteria();
        criteria.andApplyIdEqualTo(loginUser.getId());
        criteria.andUserIdEqualTo(userId);

        List<SysApply> applyList = applyMapper.selectByExample(example);
        if(applyList.size() != 0){
            return -2;
        }

        apply.setUserId(userId);
        apply.setApplyId(loginUser.getId());
        apply.setApplyName(loginUser.getUsername());
        apply.setApplyAvatar(loginUser.getUserProfile());
        apply.setStatus(1);
        apply.setApplyTime(new Date());

        if (applyMapper.insert(apply) != 1){
            return -1;
        }

        return 1;
    }

    /**
     * 同意申请
     * @param id    字段KEY
     * @return
     */
    @Override
    public int agreeApply(Long id) {
        SysApply apply = applyMapper.selectByPrimaryKey(id);
        apply.setStatus(0);

        if (applyMapper.updateByPrimaryKey(apply) != 1){
            return -1;
        }
        shipService.appendFriend(apply.getApplyName());
        return 1;
    }

    /**
     * 拒绝申请
     * @param id    字段KEY
     * @return
     */
    @Override
    public int disAgree(Long id) {
        SysApply apply = applyMapper.selectByPrimaryKey(id);
        apply.setStatus(-1);

        if (applyMapper.updateByPrimaryKey(apply) != 1){
            return -1;
        }

        return 1;
    }

    /**
     * 查看申请列表
     * @return
     */
    @Override
    public List<SysApply> listApply() {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysApplyExample example = new SysApplyExample();
        SysApplyExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(loginUser.getId());

        List<SysApply> applyList = applyMapper.selectByExample(example);

        return applyList;
    }

    /**
     * 删除申请记录
     * @param id
     * @return
     */
    @Override
    public int delApply(Long id) {
        if (applyMapper.deleteByPrimaryKey(id) != 1){
            return -1;
        }
        return 1;
    }
}
